package com.txz.framework.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用配置
 * 
 * @author txz
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer
{
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${txz.profile}")
    private String profile;

    /**
     * 配置静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 本地文件上传路径 */
        // 添加/profile/**映射到上传目录
        registry.addResourceHandler("/profile/**")
                .addResourceLocations("file:" + profile + "/")
                .setCachePeriod(3600)
                .resourceChain(true);
        
        // 明确添加/profile/images/**映射到上传目录的images子目录
        registry.addResourceHandler("/profile/images/**")
                .addResourceLocations("file:" + profile + "/upload/images/")
                .setCachePeriod(3600)
                .resourceChain(true);
    }
}
